package com.bobi.tfca.ui.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import com.bobi.tfca.R
import com.bobi.tfca.databinding.ItemActivityBigPicBinding
import com.bobi.tfca.databinding.ItemArticleBigPicBinding
import com.bobi.tfca.network.response.ActivityResponse
import com.bobi.tfca.ui.mainnavigation.home.activity.ActivityFragment
import com.bobi.tfca.util.LogUtil
import com.bumptech.glide.Glide

class ActivityAdapter(val context: Fragment, var activityList: ArrayList<ActivityResponse>) :
    RecyclerView.Adapter<ActivityAdapter.ActivityViewHolder>() {

    private var mOnItemClickListener: OnItemClickListener? = null

    interface OnItemClickListener {
        fun onItemClick(position: Int)
    }

    fun setOnItemClickListener(onItemClickListener: OnItemClickListener?) {
        mOnItemClickListener = onItemClickListener
    }

    fun setItemList(activityList: ArrayList<ActivityResponse>) {
        this.activityList = activityList
    }

    inner class ActivityViewHolder(private val binding: ItemActivityBigPicBinding) :
        RecyclerView.ViewHolder(binding.root) {
        fun bind(activity: ActivityResponse) {
            val url = activity.outsidePicture
            if (url.isNotBlank() && (!"null".equals(url))) {
                Glide.with(context).load(url).placeholder(R.drawable.defaultbg_h)
                    .into(binding.bigImage)
            } else {
                Glide.with(context).load(R.drawable.defaultbg_h).into(binding.bigImage)
            }
            binding.tvTitle.text = activity.activityName
            binding.bottomInfo.tvTime.text = activity.publishTime
            binding.bottomInfo.tvRight.text = activity.activityPlace
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ActivityViewHolder {
        val itemView =
            ItemActivityBigPicBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        val viewHolder = ActivityViewHolder(itemView)
        return viewHolder
    }

    override fun getItemCount(): Int = activityList.size
    override fun onBindViewHolder(holder: ActivityViewHolder, position: Int) {
        val activity = activityList[position]

        with(holder) {
            bind(activity)
            itemView.setOnClickListener {
                mOnItemClickListener?.onItemClick(position)
            }
        }
    }

}